<!DOCTYPE html>
<html lang="zh">
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--不设置referrer，绕过防盗链机制-->
    <meta name="referrer" content="no-referrer"/>
    <link rel="stylesheet" media="screen" href="/my/login/style.css">
    <link rel="stylesheet" type="text/css" href="/my/login/reset.css"/>
</head>
<body style="padding: 0px">

<div id="particles-js">
    <div class="login">
        <div style="font-size: 30px;font-family:方正粗黑宋简体;margin-top: 30px;box-sizing:border-box;color: #333;text-align: center">
            欢迎使用
        </div>
        <div style="font-size: 24px;font-family:楷体;margin-top: 30px;margin-bottom:30px;box-sizing:border-box;color: #333;text-align: center">
            Fast权限管理系统
        </div>
        <div class="login-center clearfix">
            <div class="login-center-img"><img src="/my/login/name.png"/></div>
            <div class="login-center-input">
                <input type="text" name="name" value="" placeholder="请输入您的用户名" onfocus="this.placeholder=''"
                       onblur="this.placeholder='请输入您的用户名'" autocomplete="off"/>
            </div>
        </div>
        <div class="login-center clearfix">
            <div class="login-center-img"><img src="/my/login/password.png"/></div>
            <div class="login-center-input">
                <input type="password" name="pwd" value="" placeholder="请输入您的密码" onfocus="this.placeholder=''"
                       onblur="this.placeholder='请输入您的密码'"/>
            </div>
        </div>
        <div class="login-center clearfix">
            <div class="login-center-img"><img src="/my/login/verifyCode.png"/></div>
            <div class="login-center-input">
                <input type="text" name="verifyCode" value="" placeholder="请输入验证码" onfocus="this.placeholder=''"
                       onblur="this.placeholder='请输入验证码'" autocomplete="off"/>
            </div>
        </div>
        <div>
            <div style="float: left;margin-left: 80px"><img id='verifyCodeImg' style="cursor:pointer;"/></div>
            <div style="float: right;margin-right:70px;margin-top: 25px;font-size: 15px">
                <label for="rememberMe">记住我</label>
                <input id='rememberMe' type="checkbox" name="rememberMe"/>
            </div>
        </div>
        <div class="login-button">
            登录
        </div>
        <div class="" style="text-align: center;margin-top: 30px;color: gray">
            <a href="https://beian.miit.gov.cn" style="color: gray;"></a><br>
            <span>  Copyright&copy;2023 &nbsp;&nbsp;Fast&nbsp;&nbsp;All Rights Reserved.</span>
        </div>
    </div>
    <div class="sk-rotating-plane"></div>
</div>

<script src="/asset/layui/layui.js"></script>
<script src="/my/fast.js"></script>
<script src="/my/login/particles.min.js"></script>
<script src="/my/login/app.js"></script>
<!--<script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>-->

<script type="text/javascript">

    // 清空cookie
    clearCookie();
    // 清空localStorage
    clearLocalStorage();

    let $ = layui.$

    // 验证码
    let uuid = UUID();
    let verifyCodeImgUrl = GATEWAY_ADDRESS + '/getCaptcha?uuid=' + uuid;
    $('#verifyCodeImg').attr('src', verifyCodeImgUrl);
    $('#verifyCodeImg').click(() => {
        $('#verifyCodeImg').attr('src', verifyCodeImgUrl);
    });

    // 登录url
    let loginUrl = GATEWAY_ADDRESS + '/login';

    function hasClass(elem, cls) {
        cls = cls || '';
        if (cls.replace(/\s/g, '').length == 0) return false;
        return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
    }

    function addClass(ele, cls) {
        if (!hasClass(ele, cls)) {
            ele.className = ele.className == '' ? cls : ele.className + ' ' + cls;
        }
    }

    function removeClass(ele, cls) {
        if (hasClass(ele, cls)) {
            let newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' ';
            while (newClass.indexOf(' ' + cls + ' ') >= 0) {
                newClass = newClass.replace(' ' + cls + ' ', ' ');
            }
            ele.className = newClass.replace(/^\s+|\s+$/g, '');
        }
    }

    /*********************************************************************************************************/
    $(function () {
        // 避免在iframe里出现登录页面
        /*        if (self !== top) {
                    top.location.reload();
                }*/

        // 移动焦点至用户名
        $('input[name=name]').focus()

        // 如果发生对象的keydown事件,表示已经获得该对象的焦点
        // 当用户名按回车,焦点移至密码
        $('input[name=name]').keydown(function (e) {
            if (e.keyCode == 13) {
                $('input[name=pwd]').focus()
            }
        })
        // 当密码按回车,焦点移至验证码
        $('input[name=pwd]').keydown(function (e) {
            if (e.keyCode == 13) {
                $('input[name=verifyCode]').focus()
            }
        })
        // 当验证码按回车,登录
        $('input[name=verifyCode]').keydown(function (e) {
            if (e.keyCode == 13) {
                login()
            }
        })
        // 点击登录按钮,登录
        $(".login-button").click(function () {
            login()
        })

    })


    // 客户端ip
    let cip = ''
    // 客户端ip所在城市
    let cname = ''

    /**
     * 获取客户端ip回调函数
     * @param data
     * @returns {*}
     * @constructor
     */
    function IPCallBack(data) {
        if (data) {
            cip = data.ip;
            cname = data.addr;
        }
    }

    // 登录
    function login() {
        layui.use(
            () => {
                let $ = layui.$

                let name = $('input[name=name]').val()
                let pwd = $('input[name=pwd]').val()
                let verifyCode = $('input[name=verifyCode]').val()
                let rememberMe = $('input[name=rememberMe]')[0].checked

                if (!name || !pwd) {
                    layer.msg('用户名或密码不能为空')
                    return
                }
                if (!verifyCode) {
                    layer.msg('请输入验证码')
                    return
                }

                doLogin(cip, cname);

                // 从第三方获取登录信息
                // let loginInfo = returnCitySN || ''
                // if (loginInfo) {
                //     cip = loginInfo.cip
                //     cname = loginInfo.cname
                // }

                // /**
                //  * 从第三方获取登录信息
                //  */
                // function getLoginInfo() {
                //     $.ajax(
                //         {
                //             type: 'get',
                //             url: 'https://ipv4.icanhazip.com',
                //             async: false,
                //             success: rs => {
                //                 cip = rs;
                //                 if (cip) {
                //                     doLogin(cip, cname);
                //                 }
                //             },
                //             error: () => {
                //                 layer.msg('被跨域了，1秒后将重新刷新页面');
                //                 setTimeout(() => {
                //                     location.reload();
                //                 }, 1000);
                //             }
                //         }
                //     );
                // }

                /**
                 * 执行登录
                 * @param cip 客户端ip
                 * @param cname 客户端城市
                 */
                function doLogin(cip, cname) {
                    $.ajax(
                        {
                            type: 'post',
                            url: loginUrl,
                            data: {
                                verifyCode: verifyCode,
                                uuid: uuid,
                                username: name,
                                password: pwd,
                                rememberMe: rememberMe,
                            },
                            async: false,
                            beforeSend: xhr => {
                                // 启用加载动画
                                addClass(document.querySelector(".sk-rotating-plane"), "active");
                                document.querySelector(".login").style.display = "none";
                            },
                            success: rs => {
                                if (rs.success) {
                                    let data = rs.data;
                                    // 保存登录信息
                                    saveLoginInfo(data);
                                    // 7天免登录
                                    if (rememberMe) {
                                        setCookie(TOKEN_NAME, data.tokenInfo.tokenValue, 60 * 24 * 7);
                                    } else {
                                        setCookie(TOKEN_NAME, data.tokenInfo.tokenValue, 60 * 24 * 1);
                                    }
                                    // 进入前台首页
                                    location.href = '/views/index.html?ip=' + cip + '&cname=' + cname;
                                } else {
                                    // 移除加载动画
                                    removeClass(document.querySelector(".sk-rotating-plane"), "active")
                                    document.querySelector(".login").style.display = "block"
                                    // 刷新验证码
                                    $('#verifyCodeImg')[0].src = verifyCodeImgUrl;
                                    $('input[name=verifyCode]').val('')
                                    $('input[name=verifyCode]').focus()
                                    layer.msg(rs.msg)
                                }
                            },
                            error: () => {
                                // 移除加载动画
                                removeClass(document.querySelector(".sk-rotating-plane"), "active")
                                document.querySelector(".login").style.display = "block"
                                layer.msg("服务器出错！");
                            }
                        });
                }

                /**
                 * 保存登录信息
                 * @param data
                 */
                function saveLoginInfo(data) {
                    if (data) {
                        // 登录用户的租户id
                        setItemLocalStorage('tenantId', data.sysUser.tenantId);
                        // 登录用户名
                        setItemLocalStorage('username', data.username);
                        // 登录用户的id
                        setItemLocalStorage('userId', data.userId);
                        // 是否已保存过登录日志
                        setItemLocalStorage('isLog', false);
                    }
                }

            }
        );

    }

</script>
<script src="//whois.pconline.com.cn/ipJson.jsp" id="ipScript"></script>
</body>
</html>
